// pages/editfile/editfile.js
const app = getApp()
const db = wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: '',
    username: '预设昵称',
    usergender: '男',
    userbirth: "2021-12-25",
    imgSrc: '',
    disablebtn: true,
    gender1: ['男', '女'],
    show: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  navigateBack: function () {
    wx.navigateBack({
      delta: 1,
    })
  },

  onLoad: function (options) {
    var gender = app.globalData.usergender
    if (app.globalData.username != null && app.globalData.username != "") {
      this.setData({
        username: app.globalData.username,
        disablebtn: false
      })
    }
    if (app.globalData.usergender != null && app.globalData.usergender != "") {
      this.setData({
        usergender: app.globalData.usergender
      })
      console.log(this.data.usergender)
    }
    if (app.globalData.userbirth != null && app.globalData.userbirth != "") {
      this.setData({
        userbirth: app.globalData.userbirth
      })
      console.log(this.data.userbirth)
    }

    if (app.globalData.profile == null || app.globalData.profile == '') {
      if (gender == "男") {
        this.setData({
          imgSrc: "cloud://xly-qcqcr.786c-xly-qcqcr-1301530190/eas-image/profile_b.png"
        })
      }
      if (gender == "女") {
        this.setData({
          imgSrc: 'cloud://xly-qcqcr.786c-xly-qcqcr-1301530190/eas-image/profile_g.png'
        })
      }
      if (gender == null || gender == '') {
        this.setData({
          imgSrc: 'cloud://xly-qcqcr.786c-xly-qcqcr-1301530190/eas-image/profile_b.png'
        })
      }
    } else {
      this.setData({
        imgSrc: app.globalData.profile
      })
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */

  uploadImg() {
    console.log(1)
    this.inc()
    // 让用户选择一张图片
    wx.chooseImage({
      count: 1,
      success: chooseResult => {
        // 将图片上传至云存储空间
        var imgcount = app.globalData.profileid
        wx.cloud.uploadFile({
          // 指定上传到的云路径
          cloudPath: "eas-user/" + imgcount + '.png' + 'imgset2',
          // 指定要上传的文件的小程序临时文件路径
          filePath: chooseResult.tempFilePaths[0],
          config: {
            env: this.data.envId
          }
        }).then(res => {
          console.log('上传成功', res);
          this.setData({
            haveGetImgSrc: true,
            imgSrc: res.fileID
          });
          console.log('imgSrc' + this.data.imgSrc)
          var userid = getApp().globalData.userid
          db.collection('eas-user').doc(userid).update({
            data: {
              profile: this.data.imgSrc
            }

          })
          db.collection('eas-user').doc(userid).get({}).then(res => {
            console.log("获取账号成功", res);
            getApp().globalData.profile = res.data.profile
          })
          wx.hideLoading();
        }).catch((e) => {
          console.log(e);
          wx.hideLoading();
        });
      },
    });
  },

  clearImgSrc() {
    this.setData({
      haveGetImgSrc: false,
      imgSrc: ''
    });
  },

  inc: function () {
    db.collection('eas-user').doc('0448022461b596c301df0c8e1c0d64b8').update({
      data: {
        imgcount: _.inc(1)
      }

    })

    console.log('cg')
  },
  handleChange(e) {
    console.log(e)
    this.setData({
      userbirth: e.detail.dateString
    })
    console.log(this.data.userbirth)
    var userid = getApp().globalData.userid
    console.log(birth)
    db.collection('eas-user').doc(userid).update({
      data: {
        userbirth: this.data.userbirth
      }
    })

  },

  changeName: function () {
    wx.navigateTo({
      url: '/pages/editfile/name',
    })
  },
  changeshow: function () {
    if (this.data.show)
      this.setData({
        show: false
      })
    else {
      this.setData({
        show: true
      })
    }
  },
  changeGender: function (e) {
    var gender = (e.detail.value == 0 ? gender = '男' : gender = '女')
    this.setData({
      usergender: gender
    })
    var userid = app.globalData.userid
    db.collection('eas-user').doc(userid).update({
      data: {
        gender: gender
      },

    })
    wx.showToast({
      title: '修改成功',
    })
    this.setData({
      show: true
    })

  },

  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.username != null && app.globalData.username != "") {
      this.setData({
        username: app.globalData.username
      })
    }
    db.collection("eas-user")
      .where({
        _id: '0448022461b596c301df0c8e1c0d64b8'
      })
      .get({})
      .then(res => {
        console.log("获取账号成功", res);
        getApp().globalData.profileid = res.data[0].imgcount
        const id = getApp().globalData.profileid
        console.log(id)

      })

  },
  changeimg: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})